import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
import { getImgLb } from '../../services/config/api';
import Toast from 'tdesign-miniprogram/toast/index';

const menuData = [
  [
    {
      title: '收货地址',
      tit: '',
      url: '',
      type: 'address',
    },
    {
      title: '优惠券',
      tit: '',
      url: '',
      type: 'coupon',
    },
    {
      title: '积分',
      tit: '',
      url: '',
      type: 'point',
    },
  ],
  [
    {
      title: '帮助中心',
      tit: '',
      url: '',
      type: 'help-center',
    },
    {
      title: '客服热线',
      tit: '',
      url: '',
      type: 'service',
      icon: 'service',
    },
  ],
];

const orderTagInfos = [
  {
    title: '待付款',
    iconName: 'wallet',
    orderNum: 0,
    tabType: 5,
    status: 1,
  },
  {
    title: '待发货',
    iconName: 'deliver',
    orderNum: 0,
    tabType: 10,
    status: 1,
  },
  {
    title: '待收货',
    iconName: 'package',
    orderNum: 0,
    tabType: 40,
    status: 1,
  },
  {
    title: '待评价',
    iconName: 'comment',
    orderNum: 0,
    tabType: 60,
    status: 1,
  },
  {
    title: '退款/售后',
    iconName: 'exchang',
    orderNum: 0,
    tabType: 0,
    status: 1,
  },
];

Page({
  data: {
    showMakePhone: false,
    userInfo: wx.getStorageSync('userInfo'),
    menuData,
    orderTagInfos,
    customerServiceInfo: {},
    currAuthStep: 1,
    showKefu: true,
    versionNo: '',
  },
  properties: {
    currAuthStep: {
      type: Number,
      value: 1,
      observer(vale) {
        console.log(vale);
        this.setData({
          currAuthStep: vale,
        });
      },
    },
  },
  //页面加载
  onLoad(options) {
    console.log(options);
    this.setData({
      currAuthStep: options.currAuthStep,
    });
    //this.getVersionInfo();
    this.fetUseriInfoHandle();
  },
  //页面渲染数据
  onShow() {
    this.getTabBar().init();
    this.fetUseriInfoHandle();
  },
  onPullDownRefresh() {
    // this.init();
  },

  init() {
    this.fetUseriInfoHandle();
  },

  fetUseriInfoHandle() {
    console.log();
    var ok = false;
    if (Object.keys(wx.getStorageSync('userInfo')).length > 0) {
      ok = true;
    }
    this.setData({
      userInfo: ok ? wx.getStorageSync('userInfo') : {},
      currAuthStep: ok ? 2 : 1,
    });
  },

  onClickCell({ currentTarget }) {
    const { type } = currentTarget.dataset;

    switch (type) {
      case 'address': {
        wx.navigateTo({ url: '/pages/usercenter/address/list/index' });
        break;
      }
      case 'service': {
        this.openMakePhone();
        break;
      }
      case 'help-center': {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了帮助中心',
          icon: '',
          duration: 1000,
        });
        break;
      }
      case 'point': {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了积分菜单',
          icon: '',
          duration: 1000,
        });
        break;
      }
      case 'coupon': {
        wx.navigateTo({ url: '/pages/coupon/coupon-list/index' });
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '未知跳转',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({ url: '/pages/order/after-service-list/index' });
    } else {
      wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({ url: '/pages/order/order-list/index' });
  },

  openMakePhone() {
    this.setData({ showMakePhone: true });
  },

  closeMakePhone() {
    this.setData({ showMakePhone: false });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    console.log(this.data.currAuthStep);
    console.log(this.data.userInfo);
    console.log(wx.getStorageSync('userInfo').id);
    if (this.data.currAuthStep === 2) {
      var jsonstr = JSON.stringify(this.data.userInfo);
      //已登录，用户详情页
      wx.navigateTo({ url: `/pages/usercenter/person-info/index?userInfo=${jsonstr}` });
    } else if (this.data.currAuthStep === 1) {
      //登录页面跳转
      wx.navigateTo({ url: '/pages/login/index' });
      console.log('登录');
    }
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },
});
